package fr.afcepf.ai89.esharing.data;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import fr.afcepf.ai89.esharing.entity.Aem;
import fr.afcepf.ai89.esharing.entity.ImgAem;
import fr.afcepf.ai89.esharing.entity.Proprietaire;

@Stateless
public class DAOImgAem implements IDAOImgAem {

	@PersistenceContext(unitName = "ESharingPhase22")
	EntityManager em;
	
	@Override
	public ImgAem getImgAem(int idImgAem) {
		
		Query hql = em.createQuery("SELECT i FROM ImgAem i WHERE i.idImgAem = :idImg");
		
		hql.setParameter("idImg", idImgAem);
		return (ImgAem) hql.getSingleResult();
		
	}

	@Override
	public List<ImgAem> getImgsAems() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ImgAem> getImgsAems(Aem aem) {
		
		Query hql = em.createQuery("SELECT i FROM ImgAem i WHERE i.aem.idAem = :param");
		
		hql.setParameter("param", aem.getIdAem());
		return (List<ImgAem>) hql.getResultList();
		
	}

	@Override
	public List<ImgAem> getImgsAems(Proprietaire prop) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int addImgAem(ImgAem imgAem) {
		
		em.persist(imgAem);
		
		Query hql = em.createQuery("SELECT MAX(idImgAem) FROM ImgAem i");
		int img = (Integer)hql.getSingleResult();
		
		return img;
		
	}

	@Override
	public int majImgAem(ImgAem imgAem) {
		
		ImgAem newImgAem = em.find(ImgAem.class, imgAem.getIdImgAem());
		newImgAem.setAem(imgAem.getAem());
		newImgAem.setLienImgAem(imgAem.getLienImgAem());
		em.merge(newImgAem);
		return newImgAem.getIdImgAem();
	}

}
